package com.situ.web.controller;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.web.entity.Student;
import com.situ.web.service.StudentService;
import com.situ.web.util.JDBCUtil;
@WebServlet("/student")
public class StudentServlet extends HttpServlet {
	
	private StudentService studentService = new StudentService();
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("StudentServlet.service()");
		// http://localhost:8080/Java1912Web/deleteStudent?id=12
		// http://localhost:8080/Java1912Web/student?method=deleteById&id=12
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
		String method = req.getParameter("method");
		//如果没有传递method参数，或参数不对，默认执行查找所有
		if (method == null || "".equals(method)) {
			method = "findAll";
		}
		switch (method) {
		case "findAll":
			findAll(req,resp);
			break;
		case "deleteById":
			deleteById(req,resp);
			break;
		case "insertStudent":
			insertStudent(req,resp);
			break;
		case "updateStudent1":
			updateStudent1(req,resp);
			break;
		case "updateStudent2":
			updateStudent2(req,resp);
			break;

		default:
			break;
		}
		
	}
	
	private void updateStudent2(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		// TODO Auto-generated method stub
		String id = req.getParameter("IdUpdate");
		System.out.println("id = "+id);
		String name = req.getParameter("NameUpdate");
		System.out.println("name = "+name);
		String age = req.getParameter("AgeUpdate");
		System.out.println("age = "+age);
		String gender = req.getParameter("GenderUpdate");
		System.out.println("gender = "+gender);
		Student student = new Student(Integer.parseInt(id), name, Integer.parseInt(age), gender);
		
		boolean isSuccess = studentService.updateStudent(student);
		
		resp.sendRedirect(req.getContextPath()+"/student?method=findAll");
	}

	//修改
	private void updateStudent1(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int SelectId = Integer.parseInt(req.getParameter("id")); 
		
		Student student = studentService.selectById(SelectId);
		
		req.setAttribute("student", student);
		req.getRequestDispatcher("/student_update.jsp").forward(req, resp);
	}

	//添加
	private void insertStudent(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		// TODO Auto-generated method stub
		
		
		String id = req.getParameter("IdInput");
		System.out.println("id:" + id);
		String name = req.getParameter("NameInput");
		System.out.println("name:" + name);
		String age = req.getParameter("AgeInput");
		System.out.println("age:" + age);
		String gender = req.getParameter("GenderInput");
		System.out.println("gender:" + gender);
		Student student = new Student(Integer.parseInt(age),name,Integer.parseInt(age),gender);
		
		
		boolean isSuccess = studentService.InsertStudent(student);
		
		resp.sendRedirect(req.getContextPath() + "/student");
	
	}

	
	//删除
	private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		// TODO Auto-generated method stub
		int id = Integer.parseInt(req.getParameter("id"));
		System.out.println("id:" + id);
		
		boolean isSuccess = studentService.deleteById(id);
		
		resp.sendRedirect(req.getContextPath() + "/student");
	}


	//查找
	private void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		List<Student> list =  studentService.findAll();
		
		//将查询出来的list放到req里面
		req.setAttribute("list", list);
		//然后转发到删除页面展示
		req.getRequestDispatcher("student_list.jsp").forward(req, resp);
	}
	
}
